#include "sysmainwindow.h"
#include "mainwindow.h"
#include <QSplitter>
#include <QTextBrowser>
#include <QHBoxLayout>
#include <QTreeView>
#include "userconfigs.h"
#include <QFile>
#include "qapplication.h"

CNS_SYS_REGISTER(SysMainWindow)

SysMainWindow::SysMainWindow()
{

}

void setStyle(const QString &fileName)
{
    if (!fileName.isEmpty()) {
        QFile file(fileName);

        if (file.open(QFile::ReadOnly)) {
            QString str = file.readAll();
            QString paletteColor = str.mid(20, 7);
            qApp->setPalette(QPalette(QColor(paletteColor)));
            qApp->setStyleSheet(str);
        }
    }
}

void SysMainWindow::f_createView(CnsComponentPtr com, CnsComponentPtr mainView)
{
    MainWindowPtr mainWindow = com->propertyInNs("mainWindow").value<MainWindowPtr>();
    UserConfigsPtr userConfigs = com->propertyInNs("userConfigs").value<UserConfigsPtr>();
    QWidget * widget = mainWindow->centralWidget();

    QSplitter *pSplitter = new QSplitter(Qt::Orientation::Horizontal, widget);
    QTreeView* pTreeView = mainWindow->findChild<QTreeView*>("trvDir");
    QTextBrowser* pTextBrower = mainWindow->findChild<QTextBrowser*>("tbFileContent");

    QHBoxLayout * pHBox = static_cast<QHBoxLayout*>(widget->layout());

    pSplitter->addWidget(pTreeView);
    pSplitter->addWidget(pTextBrower);
    pHBox->addWidget(pSplitter);

    userConfigs->loadConfigFile();

    setStyle(":/qss/black.css");

    mainWindow->show();
}
